Geolocation in iOS by Alasdair Allan

Geolocation in iOS by Alasdair Allan

Author:Alasdair Allan [Alasdair Allan]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Data Transmission Systems / Wireless
ISBN: 9781449308438
Publisher: O'Reilly Media
Published: 2012-10-02T04:00:00+00:00


Annotating Maps

Adding simple map annotations using the Map Kit framework is actually pretty easy. The first thing you need to do is create a class that implements the MKAnnotation delegate protocol. Right-click the Location group in the Project Navigator panel and New File to create a new Objective-C class (make it an NSObject subclass). Name the new class SimpleAnnotation when prompted.

Open the SimpleAnnotation.h interface file Xcode just created in the Standard editor and modify it as follows:

#import <Foundation/Foundation.h> @interface SimpleAnnotation : NSObject <MKAnnotation> @property (nonatomic, assign) CLLocationCoordinate2D coordinate; @property (nonatomic, strong) NSString *title; @property (nonatomic, strong) NSString *subtitle; + (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord; - (id)initWithCoordinate:(CLLocationCoordinate2D)coord; @end

Then open the corresponding SimpleAnnotation.m implementation file and make the changes shown:

#import "SimpleAnnotation.h" @implementation SimpleAnnotation @synthesize coordinate=_coordinate; @synthesize title=_title; @synthesize subtitle=_subtitle; + (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord { return [[[self class] alloc] initWithCoordinate:coord]; } - (id)initWithCoordinate:(CLLocationCoordinate2D)coord { if ( self = [super init] ) { self.coordinate = coord; } return self; } @end

The SimpleAnnotation class is just a container; it implements the MKAnnotation protocol to allow it to hold the coordinates and title (with subtitle) of our annotation.

Click the ViewController.h interface file to open in the Xcode editor, and import the SimpleAnnotation header file:

#import "SimpleAnnotation.h"

Then, in the viewDidLoad: method, add two annotations to our mapView as follows:

CLLocationCoordinate2D moffett = {37.4163, −122.0519}; SimpleAnnotation *moffettAnnotation = [[SimpleAnnotation alloc] initWithCoordinate:moffett]; moffettAnnotation.title = @"Moffett Federal Airfield"; moffettAnnotation.subtitle = @"37.4163, −122.0519"; [self.mapView addAnnotation: moffettAnnotation]; CLLocationCoordinate2D sanJose = {37.3647, −121.9338}; SimpleAnnotation *sanJoseAnnotation = [[SimpleAnnotation alloc] initWithCoordinate:sanJose]; sanJoseAnnotation.title = @"San Jose International"; sanJoseAnnotation.subtitle = @"37.3647, −121.9338"; [self.mapView addAnnotation: sanJoseAnnotation];

Save your changes, and change to the AppDelagate.m. You’ll have to expand your view a little bit as your map window currently excludes your new markers. Go into the animateMap: method and change the size of your view from 2 miles to 20 miles.

double miles = 20.0;

Then click the Run button in the Xcode toolbar to compile and deploy your application in the iPhone Simulator. If all goes well, you should see something much like Figure 3-6.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.